package cn.zh.stan.activity;

import android.app.Dialog;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import cn.zh.stan.BaseUrl;
import cn.zh.stan.MyApplication;
import cn.zh.stan.R;
import cn.zh.stan.util.Loading;
import cn.zh.stan.util.RegexUtils;
import cn.zh.stan.util.ToastUtil;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class PsChange extends BaseActivity {

    ImageView ps_back;
    LinearLayout changeps,clps;

    MyApplication mapp;
    String uid;

    TextView pstxt;
    private Dialog LDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ps_change);
        mapp= (MyApplication) getApplication();
        uid=mapp.getSp().getString("uid","");
        intv();
        hasps();
       }


    public void intv(){
        ps_back= (ImageView) findViewById(R.id.ps_back);
        changeps= (LinearLayout) findViewById(R.id.changeps);
        clps= (LinearLayout) findViewById(R.id.clps);
        pstxt= (TextView) findViewById(R.id.pstxt);
        changeps.setOnClickListener(this);
        clps.setOnClickListener(this);
        ps_back.setOnClickListener(this);

       }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        int id=view.getId();
        switch (id){
            case R.id.ps_back:
                finish();
                break;
            case R.id.changeps:
                //重置支付密码
                Intent intent=new Intent(PsChange.this,PayPass.class);
                startActivity(intent);
                break;
            case R.id.clps:
                //重置登录密码
                LDialog= Loading.createLoadingDialog(this,"加载中...");
                getPost();
                break;
            }
         }



    public void hasps(){
        OkHttpUtils.get(BaseUrl.HasPayPwd)
                .params("userId",uid)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        System.out.println("---------->是否有支付密码=="+s);
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean State=js.getBoolean("State");
                            if (State){
                              boolean Res=js.getBoolean("Res");
                                System.out.println("------->Res=="+Res);
                                 if (!Res){
                                    pstxt.setText("设置支付密码");
                                 }else {
                                     pstxt.setText("重置支付密码");
                                 }
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
             }

         //重置登录密码
         public void getPost() {
             final String moblie =mapp.getSp().getString("phone","");
             boolean ismobile = RegexUtils.checkMobile(moblie);
             if (ismobile){
                 OkHttpUtils.post(BaseUrl.pass_find)
                         .params("moblie", moblie)
                         .execute(new StringCallback() {
                             @Override
                             public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                                 System.out.println("------------->忘记密码发送验证码==" + s);
                                 try {
                                     Loading.closeDialog(LDialog);
                                     JSONObject js = new JSONObject(s);
                                     boolean status = js.getBoolean("State");
                                     if (status) {
                                         boolean Flag = js.getBoolean("Flag");
                                         boolean Mark = js.getBoolean("Mark");
                                         if (Flag) {
                                             Intent intent = new Intent(PsChange.this, FinPasst.class);
                                             intent.putExtra("name", moblie);
                                             startActivity(intent);
                                         }
                                         String Msg = js.getString("Msg");
                                         ToastUtil.showShort(PsChange.this, Msg);
                                     } else {
                                         String msg = js.getString("msg");
                                         ToastUtil.showLong(PsChange.this, msg);
                                     }
                                 } catch (JSONException e) {
                                     e.printStackTrace();
                                 }
                             }
                         });
             }else {
                 ToastUtil.showLong(PsChange.this,"请完善用户资料");
             }
         }







}
